package com.bluetrum.abmate.leaudio;

import android.os.Bundle;
import android.util.Pair;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.bluetrum.abmate.R;
import com.bluetrum.abmate.cmd.SimpleRequestCallback2;
import com.bluetrum.abmate.databinding.ActivityLeAudioBisBinding;
import com.bluetrum.abmate.utils.Utils;
import com.bluetrum.abmate.viewmodels.SharedViewModel;
import com.bluetrum.devicemanager.cmd.Request;
import com.bluetrum.devicemanager.cmd.request.LeAudioBisSelectRequest;
import com.bluetrum.devicemanager.cmd.request.LeAudioBisStatusRequest;
import com.bluetrum.devicemanager.models.LeAudioBisItem;

import java.util.Date;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;
import timber.log.Timber;

@AndroidEntryPoint
public class LeAudioBisActivity extends AppCompatActivity
        implements LeAudioBisAdapter.OnItemClickListener {

    private SharedViewModel mViewModel;

    private ActivityLeAudioBisBinding binding;
    private LeAudioBisAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        binding = ActivityLeAudioBisBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setTitle(R.string.function_le_audio_bis);

        setupListView();
        setupObservers();
    }

    @Override
    protected void onStart() {
        super.onStart();
        startUpdateLeAudioBis(true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        startUpdateLeAudioBis(false);
        // 不逐层调用了，直接在这里清空列表
        List<Pair<Date, LeAudioBisItem>> items = mViewModel.getLeAudioBisList().getValue();
        if (items != null) {
            items.clear();
        }
    }

    private void setupListView() {
        RecyclerView recyclerView = binding.recyclerView;

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(dividerItemDecoration);

        final SimpleItemAnimator itemAnimator = (SimpleItemAnimator) recyclerView.getItemAnimator();
        if (itemAnimator != null) itemAnimator.setSupportsChangeAnimations(false);

        adapter = new LeAudioBisAdapter();
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    private void setupObservers() {
        mViewModel.getLeAudioBisList().observe(this, list -> {
            if (list != null) {
                adapter.updateList(list);
            } else {
                adapter.removeAll();
            }
        });
    }

    @Override
    public void onItemClick(final View view, final LeAudioBisItem bisItem) {
        // 发送选择请求
        boolean toConnect = !Boolean.TRUE.equals(bisItem.isConnected());
        Request request = new LeAudioBisSelectRequest(bisItem.getIdentifier(), toConnect);
        mViewModel.sendRequest(request, new SimpleRequestCallback2() {
            @Override
            public void onComplete(boolean result) {
                Timber.d("sendRequest result = %s", result);

                if (result) {
                    Utils.showToast(LeAudioBisActivity.this, R.string.request_succeeded);
                } else {
                    Utils.showToast(LeAudioBisActivity.this, R.string.request_failed);
                }
            }

            @Override
            public void onTimeout() {
                Timber.w("Request timeout");
                Utils.showToast(LeAudioBisActivity.this, R.string.request_timeout);
            }
        });
    }

    private void startUpdateLeAudioBis(boolean start) {
        Request request = new LeAudioBisStatusRequest(start);
        mViewModel.sendRequest(request, new SimpleRequestCallback2() {
            @Override
            public void onComplete(boolean result) {
                Timber.d("sendRequest result = %s", result);

                if (result) {
                    Utils.showToast(LeAudioBisActivity.this, R.string.request_succeeded);
                } else {
                    Utils.showToast(LeAudioBisActivity.this, R.string.request_failed);
                }
            }

            @Override
            public void onTimeout() {
                Timber.w("Request timeout");
                Utils.showToast(LeAudioBisActivity.this, R.string.request_timeout);
            }
        });
    }
}